package main

import (
	"context"
	"go-pioneer/config"
	"go-pioneer/pkg/wire"
	"go.uber.org/zap"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	// 初始化配置
	if err := config.InitialConfiguration(); err != nil {
		panic(err)
	}
	engine := wire.InitWebServer()
	srv := &http.Server{
		Addr:    ":9001",
		Handler: engine,
	}

	zap.S().Info("Starting server on " + srv.Addr)

	if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
		zap.S().Errorf("Server failed to start: %v", err)
	}

	quit := make(chan os.Signal, 1)
	signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
	<-quit
	zap.L().Info("Shutting down server...")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	if err := srv.Shutdown(ctx); err != nil {
		zap.S().Errorf("Server forced to shutdown:", err)
	}
}
